1   /**
2    * Copyright 2014 Netflix, Inc.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package rx.internal.operators;
17  
18  import rx.Observable.Operator;
19  import rx.Subscriber;
20  import rx.functions.Func1;
21  
22  /**
23   * Returns an Observable that emits a Boolean that indicates whether all items emitted by an
24   * Observable satisfy a condition.
25   * <p>
26   * <img width="640" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/all.png" alt="">
27   */
28  public final class OperatorAll<T> implements Operator<Boolean, T> {
29      private final Func1<? super T, Boolean> predicate;
30  
31      public OperatorAll(Func1<? super T, Boolean> predicate) {
32          this.predicate = predicate;
33      }
34  
35      @Override
36      public Subscriber<? super T> call(final Subscriber<? super Boolean> child) {
37          Subscriber<T> s = new Subscriber<T>() {
38              boolean done;
39  
40              @Override
41              public void onNext(T t) {
42                  boolean result = predicate.call(t);
43                  if (!result && !done) {
44                      done = true;
45                      child.onNext(false);
46                      child.onCompleted();
47                      unsubscribe();
48                  } else {
49                  	// if we drop values we must replace them upstream as downstream won't receive and request more
50                  	request(1);
51                  }
52              }
53  
54              @Override
55              public void onError(Throwable e) {
56                  child.onError(e);
57              }
58  
59              @Override
60              public void onCompleted() {
61                  if (!done) {
62                      done = true;
63                      child.onNext(true);
64                      child.onCompleted();
65                  }
66              }
67          };
68          child.add(s);
69          return s;
70      }
71  }